<?php

require_once 'classes/FUtil.class.php';

FUtil::loadClasses();

$session = new FSessionManager($filters['detailName']);
$response = new FJSONResponse();
$filters = $_POST;

if ( $filters['action'] == 'list' )
{
    $response->setSuccess(true);
    
    if ( $filters['formAction'] == FEditForm::FUNCTION_UPDATE )
    {
        if ( strlen($filters['masterIndex']) > 0 )
        {
            $data = $session->getDataAt($filters['masterIndex']);
            
            $rows = NULL;
            foreach ( $data as $key => $viewRow )
            {
                if ( $viewRow instanceof FDetailRow )
                {
                    if ( $viewRow->getMode() == FDetailRow::VIEW_ROW )
                    {
                        $rows[$key] = NULL;
                    }
                    else
                    {
                        $rows[$key] = $viewRow;
                    }
                }
            }
            
            //$session->setDataAt($filters['masterIndex'], $rows);
            
            $detailController = new FDefaultController();
            $detailController->setTable($filters['detailName']);
            $detailController->setFilters(array(
                $filters['masterIndexName'] => $filters['masterIndex']
            ));
            
            $detailData = $detailController->search();
            foreach ( $detailData as $detailRowData )
            {
                $foreignController = new FDefaultController();
                $foreignController->setTable($filters['detailTable']);
                $foreignController->setFilters(array(
                    'idxName' => $filters['detailIndexName'],
                    'dataId' => $detailRowData[$filters['dataId']]
                ));
                
                $foreignRow = $foreignController->get();                
                $foreignRow = array_merge($foreignRow, $filters);
                
                $rows[] = new FDetailRow(FDetailRow::VIEW_ROW, $foreignRow);
            }
            
            $session->setDataAt($filters['masterIndex'], $rows);
            $rows = NULL;
            
            $data = $session->getDataAt($filters['masterIndex']);
        }
    }
    else
    {
        $data = $session->getDataAt($filters['manager']);
    }
    
    foreach ( $data as $row )
    {
        if ( $row instanceof FDetailRow )
        {
            if ( $row->getMode() == FDetailRow::VIEW_ROW || $row->getMode() == FDetailRow::NEW_ROW )
            {
                $rows[] = $row->getData();
            }
        }
    }
    
    $response->setData($rows);
}
elseif ( $filters['action'] == 'add' )
{    
    $filters[$filters['masterIndexName']] = $filters['masterIndex'];
    
    $detailController = new FDefaultController();
    $detailController->setTable($filters['detailTable']);
    $detailController->setFilters(array(
        'idxName' => $filters['detailIndexName'],
        'dataId' => $filters[$filters['dataId']]
    ));

    $detailRow = $detailController->get();
    $detailRow = array_merge($detailRow, $filters);
    
    
    if ( $filters['formAction'] == FEditForm::FUNCTION_UPDATE )
    {
        $data = $session->getDataAt($filters['masterIndex']);
        $data[] = new FDetailRow(FDetailRow::NEW_ROW, $detailRow);
        $session->setDataAt($filters['masterIndex'], $data);
    }
    else
    {
        $data = $session->getDataAt($filters['manager']);
        $data[] = new FDetailRow(FDetailRow::NEW_ROW, $detailRow);
        $session->setDataAt($filters['manager'], $data);
    }
        
    $response->setSuccess(true);
    $response->setShowSuccessMsg(FALSE);
}
elseif ( $filters['action'] == 'save' )
{
    $controller = new FDefaultController();
    
    $data = $session->getDataAt($filters['masterIndex']);
    
    $foreignTables = $controller->obterTabelasEstrangeiras($filters['manager']);
    foreach ( $foreignTables as $foreignTable )
    {
        if ( $foreignTable['table'] == $filters['detailName'] )
        {
            $detailController = new FDefaultController();
            $detailController->setTable($foreignTable['table']);
            
            foreach ( $data as $row )
            {
                if ( $row instanceof FDetailRow )
                {
                    if ( $row->getMode() == FDetailRow::NEW_ROW )
                    {
                        $detailController->setFilters($row->getData());
                        $detailController->insert();
                    }
                }
            }
        }
    }
    
    $session->setDataAt($filters['masterIndex'], NULL);
}
elseif ( $filters['action'] == 'clear' )
{
    if ( $filters['masterIndex'] )
    {
        $session->setDataAt($filters['masterIndex'], NULL);
    }
    else
    {
        $session->setDataAt($filters['manager'], NULL);
    }
}

$response->response();

?>
